<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\CategoryRepositories;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    public function __construct(CategoryRepositories $categoryRepositories)
    {
        $this->categoryRepositories=$categoryRepositories;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.category.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $id =$request->input('id');
        $type =$request->input('type','post');
        $categorys= $this->categoryRepositories->allCategory($type);

        return view('admin.category.create',compact('categorys','id','type'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'title'  => 'required|string',
            'type'  => 'required|string',
        ],[
            'title.required'=>'* 名称必传!',
            'type.required'=>'* 所属类型必选!',
        ]);
        $ret = $this->categoryRepositories->save($request->all());

        if($ret)
        {
            if($request->input('type') =='goods')
            {
                return redirect()->route('admin.goods.category')->with(['success'=>'添加完成']);
            }
            return redirect()->route('admin.category')->with(['success'=>'添加完成']);

        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request,$id)
    {
        $type =$request->input('type','post');
        $categorys= $this->categoryRepositories->allCategory($type);
        $info =$this->categoryRepositories->find($id);

        return view('admin.category.edit',compact('info','categorys','type'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->only(['title','slug']);
        $ret = $this->categoryRepositories->update($id,$data);

        if($ret)
        {
            return redirect()->route('admin.category')->with(['success'=>'修改成功！']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        $ids=request()->input('ids');

        if(empty($ids))
        {
            return response()->json([
                'code' => 0,
                'msg'   => '请选择删除项！',
            ]);
        }

        if(!is_array($ids))
        {
            return response()->json([
                'code' => 0,
                'msg'   => '非法参数！',
            ]);
        }
        foreach ($ids as $key => $val)
        {
            $category =$this->categoryRepositories->find($val);

            if (!$category){
                return response()->json(['code'=>-1,'msg'=>'请选择删除项']);
            }
            if (!$category->childs->isEmpty()){
                return response()->json(['code'=>-1,'msg'=>'该分类下有子分类或者文章，不能删除']);
            }
        }

        $ret =$this->categoryRepositories->delete($ids);
        if($ret)
        {
            return response()->json([
                'code' => 0,
                'msg'   => '删除成功！',
            ]);
        }
        return response()->json([
            'code' => -1,
            'msg'   => '删除失败！',
        ]);
    }

    public function data(Request $request)
    {
        $data = $request->all();
        //$res = $this->categoryRepositories->search($data);
        $categorys= $this->categoryRepositories->allCategory($data['type']);

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => 0,
            'data'  => $categorys
        ];
        return response()->json($data);
    }
}
